﻿using UnityEngine;
using System.Collections;

public class ContactDamage : MonoBehaviour {

	public float Damage = 0;
	///The lifetime of the attack, in seconds.
	///Set to 0 to indicate infinite length attack. 
	/// <summary>
	/// If true, removes the weapon from the game world on impact.
	/// </summary>
    public Vector3 AttackDirection = Vector3.zero;
    public float KnockbackForce = 120.0f;
    public float DamageDelay = 0.0f;
    private float damageCountdown = 0.0f;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
        damageCountdown -= Time.deltaTime;
	}

	void OnCollisionEnter(Collision col)
	{
		//TODO: spawn dust flare & explosion
		//inflict damage if possible
        GameObject other = col.gameObject;
		HealthInfo hitHealth = other.transform.GetComponent<HealthInfo>();
        if (hitHealth != null)
        {
            hitHealth.ApplyDamage(Damage);
            //also knock the entity back if possible!
            Rigidbody targetRb = hitHealth.rigidbody;
            if (targetRb != null)
            {
                targetRb.AddForce(AttackDirection * KnockbackForce);
            }
            damageCountdown = DamageDelay;
        }
	}
}
